package org.crumbleworks.crumbchat.server.controller;

import java.io.IOException;

import org.crumbleworks.crumbchat.common.data.Message;
import org.crumbleworks.crumbchat.server.ChatServer;
import org.crumbleworks.crumbchat.server.InputParser;
import org.crumbleworks.crumbchat.server.ServerListener;

public class ServerController implements ServerControllerInterface {
    private ServerListener serverListener;
    private ChatServer chatServer;

    private GuiController guiController;
    private ConsoleController consoleController;

    private InputParser inputParser;

    private boolean guiEnabled;
    private boolean consoleEnabled;

    public ServerController(boolean guiEnabled, boolean consoleEnabled) {
        this.guiEnabled = guiEnabled;
        this.consoleEnabled = consoleEnabled;

        serverListener = new ServerListener(this);

        chatServer = new ChatServer(44444, 44444);
        chatServer.addListener(serverListener);

        inputParser = new InputParser(this);

        if (guiEnabled) {
            guiController = new GuiController(this);
        }
        if (consoleEnabled) {
            consoleController = new ConsoleController(this);
        }
    }

    @Override
    public void bindPorts(int tcpPort, int udpPort) {
        chatServer.setTcpPort(tcpPort);
        chatServer.setUdpPort(udpPort);

        try {
            chatServer.bind();
        }
        catch (IOException ex) {
            showBindError(tcpPort, udpPort);
        }
    }

    @Override
    public void unbindPorts(boolean shutdownAfterUnbind) {
        chatServer.unbind();

        if (shutdownAfterUnbind) {
            shutdown();
        }
    }

    @Override
    public void sendMessageToClients(String text) {
        Message message = new Message("[server]", text);

        chatServer.sendToAllTCP(message);
        chatServer.sendToAllUDP(message);
        putMessageToChat(message);
    }

    @Override
    public void putMessageToChat(Message message) {
        if (guiEnabled) {
            guiController.putMessageToChat(message);
        }
        if (consoleEnabled) {
            consoleController.putMessageToChat(message);
        }
    }

    @Override
    public void showBindError(int tcpPort, int udpPort) {
        if (guiEnabled) {
            guiController.showBindError(tcpPort, udpPort);
        }
        if (consoleEnabled) {
            consoleController.showBindError(tcpPort, udpPort);
        }
    }

    @Override
    public void shutdown() {
        if (guiEnabled) {
            guiController.shutdown();
        }
        if (consoleEnabled) {
            consoleController.shutdown();
        }

        System.exit(0);
    }

    @Override
    public void parseInput(String input) {
        inputParser.parseInput(input);
    }

    public static void main(String[] args) {
        new ServerController(true, true);
    }
}
